#ifndef _INCLUDE_POINTER_ARRAY_
#define _INCLUDE_POINTER_ARRAY_

#include "types.h"

//! Onet uses a lot of pointer-arrays
//! This class makes it a lot safer and easier to use

namespace onet
{

    template<class T> class PointerArray
    {
    public:
        PointerArray() : size(-1)
        {

        }

        PointerArray(u16 size_) : size(-1)
        {
            this->init(size_);
        }

        bool init(u16 size_)
        {
            if (size > 0 || size_ == 0)
                return false;

            size = size_;

            data = new T*[size];

            for (u16 u = 0; u < size; u++)
                data[u] = 0;

            return true;
        }

        ~PointerArray()
        {
            this->clear();

            delete []data;
        }

        void clear()
        {
            if (size < 0)
                return;

            for (u16 u = 0; u < size; u++)
            {
                if (data[u])
                    delete data[u];

                data[u] = 0;
            }
        }

        u16 getSize()
        {
            return size;
        }

        T*& getValue(u16 pos)
        {
            return data[pos];
        }

        T*& operator[] (u16 pos)
        {
            return this->getValue(pos);
        }

        void setValue(u16 pos , T* value, bool replace = false)
        {
            if (!replace && data[pos])
                delete value;
            else
            {
                data[pos] = value;
            }
        }

        void remove(u16 pos)
        {
            if (pos >= size)
                return;

            if (data[pos])
            {
                delete data[pos];
                data[pos] = 0;
            }
        }

    private:
        T** data;
        s32 size;
    };

} //! end namespace onet

#endif
